package com.kh.samr.controller;

import com.alibaba.fastjson.JSONObject;
import com.hy.gs.itf.biz.RequestParams;
import com.hy.gs.itf.biz.http.HttpUtils;
import com.kh.samr.dto.*;
import com.kh.samr.entity.DishonestyParam;
import com.kh.samr.service.IDishonestyParamService;
import com.kh.samr.service.IDishonestyService;
import com.kh.samr.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

/**
 * 失信黑名单接口
 *
 * @author Harry
 * @date 2020/12/14
 */
@SuppressWarnings("DuplicatedCode")
@Api(tags = "失信黑名单接口")
@RestController
public class DishonestyController {

	private static final Logger log = LoggerFactory.getLogger(DishonestyController.class);

	@Resource
	IDishonestyService dishonestyService;

	@Resource
	IDishonestyParamService dishonestyParamService;

	@Value("${AccessKeySecret}")
	String accessKeySecret;

	@ApiOperation(value = "查询所有失信黑名单", notes = "findAll")
	@ApiImplicitParam(name = "accessKeySecretDto",
			value = "访问授权凭证字符串Dto", required = true, dataType = "AccessKeySecretDto")
	@PostMapping("/get_all_dishonesty")
	public ResultUtil<List<DishonestyListOldDto>> findAll(
			@RequestBody @Valid AccessKeySecretDto accessKeySecretDto) {
		log.info("方法名称：" + "findAll" +
				";参数：accessKeySecretDto:" + accessKeySecretDto.toString());
		if (StringUtils.equals(this.accessKeySecret, accessKeySecretDto.getAccessKeySecret())) {
			ResultUtil<List<DishonestyListOldDto>> result = this.dishonestyService.findAll();
			log.info("方法名称：" + "findAll" + ";返回值：" + result.toString());
			return result;
		} else {
			ResultUtil<List<DishonestyListOldDto>> result = new ResultUtil<>(1, "访问授权凭证错误");
			log.error("方法名称：" + "findAll" + ";返回值：" + result);
			return result;
		}
	}

	@ApiOperation(value = "根据统一社会信用代码查询失信黑名单", notes = "findAll")
	@ApiImplicitParam(name = "byUniformSocialCreditCodeDto",
			value = "根据统一社会信用代码查询Dto", required = true, dataType = "ByUniformSocialCreditCodeDto")
	@PostMapping("/get_all_dishonesty_by_uniform_social_credit_code")
	public ResultUtil<List<DishonestyListOldDto>> findAllByUniformSocialCreditCode(
			@RequestBody @Valid ByUniformSocialCreditCodeDto byUniformSocialCreditCodeDto) {
		log.info("方法名称：" + "findAllByUniformSocialCreditCode" +
				";参数：byUniformSocialCreditCodeDto:" + byUniformSocialCreditCodeDto.toString());
		if (StringUtils.equals(this.accessKeySecret, byUniformSocialCreditCodeDto.getAccessKeySecret())) {
			ResultUtil<List<DishonestyListOldDto>> result = this.dishonestyService
					.findAllByUniformSocialCreditCode(byUniformSocialCreditCodeDto.getUniformSocialCreditCode());
			log.info("方法名称：" + "findAll" + ";返回值：" + result.toString());
			return result;
		} else {
			ResultUtil<List<DishonestyListOldDto>> result = new ResultUtil<>(1, "访问授权凭证错误");
			log.error("方法名称：" + "findAll" + ";返回值：" + result);
			return result;
		}
	}

	@ApiOperation(value = "根据统一社会信用代码和企业名称查询失信黑名单", notes = "根据统一社会信用代码和企业名称查询失信黑名单")
	@ApiImplicitParam(name = "dishonestyParamDto",
			value = "红黑名单查询参数Dto", required = true, dataType = "DishonestyParamDto")
	@PostMapping("/getByDishonestyParam")
	public ResultUtil<List<DishonestyListDto>> findByParam(
			@RequestBody @Valid DishonestyParamDto dishonestyParamDto) {
		log.info("方法名称：" + "findByParam" +
				";参数：dishonestyParamDto:" + dishonestyParamDto.toString());
		ResultUtil<List<DishonestyListDto>> result = new ResultUtil<>();
		if (StringUtils.equals(this.accessKeySecret, dishonestyParamDto.getAccessKeySecret())) {
			DishonestyParam dishonestyParam = new DishonestyParam();
			BeanUtils.copyProperties(dishonestyParamDto, dishonestyParam);
			this.dishonestyParamService.save(dishonestyParam);
			RequestParams requestParams = new RequestParams();
			requestParams.setContext("/kjLawList/kjLawBlackList");
			requestParams.setMethod(RequestParams.POST);
			requestParams.addParam("name", dishonestyParam.getEnterpriseName());
			requestParams.addParam("uniscId", dishonestyParam.getUniformSocialCreditCode());
			requestParams.addParam("itemName", "KJJ01");
			requestParams.addParam("queryTime", new Date());
			requestParams.addParam("queryDepartment", "市科技局");
			JSONObject jsonObject = JSONObject.parseObject(HttpUtils.sendRequest(requestParams));
			List<DishonestyListDto> dishonestyListDtoList = JSONObject.parseArray(jsonObject.get("data").toString(),
					DishonestyListDto.class);
			Set<DishonestyListDto> set = new HashSet<>(dishonestyListDtoList);
			List<DishonestyListDto> dishonestyListDtoList1 = new ArrayList<>(set);
			result.setDishonestyList(dishonestyListDtoList1);
			log.info("方法名称：" + "findByParam" + ";返回值：" + result);
		} else {
			result.setErrcode(0);
			result.setErrmsg("访问授权凭证错误");
			log.error("方法名称：" + "findByParam" + ";返回值：" + result);
		}
		return result;
	}
}
